package Test;

import java.io.IOException;
import java.util.Iterator;
import Interact.Interface;
import Tool.Tool;

public class Selection extends Question{
	public static final int TYPE = 3;
	
	public Selection(){
		body = new SelectionBody();
		content = new Content();
		content.setAnswer("0");
	}
	
	class AnswerMode implements InteractMode{
		@Override
		public void display(Interface inf) throws IOException {
			SelectionBody body = (SelectionBody)getBody();
			// 题干
			inf.println(body.getCont());
			
			// 插图
			body.getIllustration().display(inf);
			
			// 选项			
			Iterator<String> iter =  body.getOptions().iterator();
			
			String format = "%s<input type=\"radio\" name=\"" + getKey() + "\" value=\"%d\"%s /><br/>";
			for (int i = 0, ans = Integer.parseInt(content.getAnswer()); iter.hasNext(); ++i){
				inf.print(String.format(format, (char)(i + 'A') + ".&nbsp;" + Tool.toHTML(iter.next()), i, i == ans ? " checked" : ""));
			}
		}
		
		@Override
		public void setValueViaInterface(Interface inf) {
			String ans = inf.getValue(getKey());
			content.setAnswer(ans == null ? "" : ans);
		}
	}
	
	class CorrectMode implements InteractMode{
		private String scoreName(){
			return getKey() + "score";
		}
		
		private String commentName(){
			return getKey() + "comment";
		}
		@Override
		public void display(Interface inf) throws IOException {
			SelectionBody body = (SelectionBody)getBody();
			// 题干
			inf.println(body.getCont());
			
			// 插图
			body.getIllustration().display(inf);
			
			// 选项
			Iterator<String> iter =  body.getOptions().iterator();
			for (int i = 0; iter.hasNext(); ++i){
				inf.printf("%c.&nbsp;%s<br/>", (char)(i+'A'), Tool.toHTML(iter.next()));
			}
			
			String ans;
			try{
				char a = (char)Integer.parseInt(content.getAnswer());
				ans = Character.toString(a += 'A');
			}
			catch (Exception e){
				ans = "不正确的答案";
			}
			
			// 满分
			inf.println("满分：" + body.getGrade());
			
			// 打印答案
			inf.println("学生答案：" + ans);
			
			// 正确答案
			inf.println("正确答案：" + (char)(body.getAns() + 'A'));
						
			// 输入得分
			inf.printf("在此输入得分(非法输入认为得分为0)：<input name=\"%s\" value=\"%d\" /><br/>", scoreName(), content.getScore());
			
			// 输入评语
			inf.printf("<textarea name=%s>%s</textarea>",
					commentName(), content.getComment());
		}
		@Override
		public void setValueViaInterface(Interface inf) {
			String value = inf.getValue(scoreName());
			try{content.setScore(Integer.parseInt(value));}
			catch (Exception e){content.setScore(0);}
			
			value = inf.getValue(commentName());
			content.setComment(value == null ? "" : value);
		}
	}
	
	@Override
	public int correct() {
		SelectionBody body = (SelectionBody)this.body;
		try{
			if (Integer.parseInt(content.getAnswer()) == body.getAns())
				content.setScore(body.getGrade());
			else content.setScore(0);
		}
		catch (Exception e){
			content.setScore(0);
		}
		
		return content.getScore();
	}
	
	

	@Override
	public int getType() {
		return TYPE;
	}

	@Override
	void initModes() {
		modes = new InteractMode[]{
			new AnswerMode(),
			new CorrectMode()
		};
	}

}
